package com.heatup.apt.api.controller;

import com.heatup.apt.api.service.ProtalService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 首页控制器
 * Created by earvin on 2017/7/13.
 */
@Controller
@RequestMapping(value = "/protal")
public class PortalController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private ProtalService protalService;

    @ResponseBody
    @RequestMapping(value = "/protalInfo")
    public String protalInfo(HttpServletRequest request, HttpServletResponse response) {
        String callback = RequestTools.RequestString(request, "callback", "");
        UserInfo userInfo  = this.getUserInfo();
        ApiResultModel model = new ApiResultModel();
        //用户未登陆
        if(!isLogined()){
            model.setErrorCode(ApiResultModel.RET_ERROR);
            model.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            model.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
            model.setLogined(isLogined());
            return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(model), callback);
        }

        //获取首页信息
        try{
            Map<String,Object> respMap = protalService.protalInfoService(userInfo);
            model.setData(respMap);
            model.setRet(ApiResultModel.RET_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("=======>>>用户首页信息获取失败: ",e);
            model.setMsg("用户首页信息获取失败: " + e);
            model.setRet(ApiResultModel.RET_ERROR);
        }

        model.setLogined(isLogined());
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(model), callback);
    }

}
